﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DWEIP.Web.Core;
using DWEIP.Core.OnlineStat;
using DWEIP.Models;
using DWEIP.Models.Sys;
using DWEIP.Models.MIS;
using DWEIP.BLL.Sys;
using DWEIP.BLL.MIS;
using DWEIP.Common;

namespace DWEIP.Web.Areas.Mis.Controllers
{
    public class WebIMController : BaseController
    {
        public SysStructBLL structBLL = new SysStructBLL();
        public SysUserBLL sysUserBLL = new SysUserBLL();
        public MIS_WebIM_MessageBLL messageBLL = new MIS_WebIM_MessageBLL();

        private SysConfigModel siteConfig = new SysConfigBLL().loadConfig(Utils.GetXmlMapPath("Configpath"));

        ValidationErrors errors = new ValidationErrors();

        // GET: MIS/WebIM
        public ActionResult Index()
        {
            //获取真实姓名
            ViewBag.UserName = GetUserTrueName();
            ViewBag.MIS0002 = siteConfig.refreshcurrentwin;
            ViewBag.MIS0003 = siteConfig.refreshrecentcontact;
            ViewBag.MIS0004 = siteConfig.refreshnewmessage;

            return View();
        }

        public ActionResult ChatLog()
        {
            return View();
        }

        /// <summary>
        /// 获取聊天记录
        /// </summary>
        [HttpPost]
        public JsonResult GetChatLogList(GridPager pager, string queryStr)
        {
            var list = messageBLL.GetMessage(ref pager, GetUserId());
            var json = new
            {
                total = pager.totalRows,
                rows = (from r in list
                        select new MIS_WebIM_MessageModel()
                        {

                            Id = r.Id,
                            Message = r.Message,
                            Sender = r.Sender,
                            receiver = r.receiver,
                            State = r.State,
                            SendDt = r.SendDt,
                            receiverTitle = r.receiverTitle

                        }).ToArray()

            };

            return Json(json);
        }

        public JsonResult GetTreeByEasyui(string id)
        {
            OnlineUserRecorder recorder = HttpContext.Cache[OnlineHttpModule.g_onlineUserRecorderCacheKey] as OnlineUserRecorder;
            List<SysStructModel> list = structBLL.GetList(id);
            List<SysUser> userList = sysUserBLL.GetListByDepId(id);
            if (userList.Count > 0)
            {
                foreach (var user in userList)
                {
                    SysStructModel addUserToStruct = new SysStructModel();
                    addUserToStruct.Id = user.Id;
                    addUserToStruct.Name = user.TrueName;
                    addUserToStruct.Type = "member";
                    list.Insert(0, addUserToStruct);//把人员加进组
                }
            }
            var json = from m in list
                       select new SysStructUserModel()
                       {
                           id = m.Id,
                           text = "<input id=\"" + m.Id + "\" class=\"" + (m.Type == "group" ? "CBGroup" : "CBMember") + "\" type=\"checkbox\" ref=\"" + id + "\" value=\"" + m.Id + "\" />" + m.Name,     //text
                           attributes = m.Name,
                           iconCls = m.Type == "group" ? "fa fa-users color-green" : SetOnlineIcon(m.Id, recorder),
                           state = m.Type == "group" ? "closed" : "open"

                       };
            return Json(json);
        }

        //判断用户是否在线
        public string SetOnlineIcon(string id, OnlineUserRecorder recorder)
        {
            if (recorder == null)
                return "fa fa-user color-gray";
            // 绑定在线用户列表
            IList<OnlineUser> userList = recorder.GetUserList();
            bool b = false;
            foreach (var OnlineUser in userList)
            {
                if (id == OnlineUser.UserName)
                {
                    b = true;
                }
            }
            if (b)
            {
                return "fa fa-user color-green";
            }
            else
            {
                return "fa fa-user color-gray";
            }
        }
    }
}